package com.example.listview;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.listviewtest.R;

public class UserAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<User> userList;

    public UserAdapter(Context context, List<User> userList) {
        inflater = LayoutInflater.from(context);
        this.userList = userList;
    }

    @Override
    public int getCount() {
        return userList == null ? 0 : userList.size();
    }

    @Override
    public Object getItem(int position) {
        return userList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.activity_main_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        User user = userList.get(position);
        holder.tvTitle.setText(user.getName());
        holder.tvContent.setText(user.getEmail());
        return convertView;
    }

    private static class ViewHolder {

        public final TextView tvTitle;
        public final TextView tvContent;

        public ViewHolder(View convertView) {
            tvTitle = (TextView) convertView.findViewById(R.id.main_item_tv_title);
            tvContent = (TextView) convertView.findViewById(R.id.main_item_tv_content);
        }

    }

}
